﻿using System.Collections.Generic;
using IntegratedMeasuring.ViewModels.Entity;
using IntegratedMeasuring.ViewModels.Scheme;

namespace IntegratedMeasuring.Utils.InstrumentParser;

public class Channel20Parser : BaseParser
{
   public static void ParseAutoTransFileContent(byte[] data, out TestConditions testConditions)
    {
        testConditions = new TestConditions();
        // 文件名称
        testConditions.Name = PraseTransName(data.GetBytesByLength(1, 16));
        testConditions.SourceData = data;
        // 所有脚位 Pin_Label
        List<string> pinLabels = PrasePinLabel(data.GetBytesByLength(11113, 84)); 
        List<PinData> pins1 = new List<PinData>();
        
        byte[] temp = data.GetBytes(52, 132);
        for (int i = 0; i < temp.Length; i++)
        {
            if (temp[i] != 0)
            {
                // string pin = $"{pinLabels[temp[i] - 1]} - {pinLabels[temp[i + 1] - 1]}";
                string pin = $"{temp[i].ToInt().ToString().PadLeft(2, '0')} - {temp[i + 1].ToInt().ToString().PadLeft(2, '0')}";
                pins1.Add(new PinData
                {
                    PRI = i / 20,
                    SEC = (i / 2) % 10,
                    Pin = pin,
                });
            }
            i++;
        }
        
        List<TransItemData> transItemDatas = new List<TransItemData>();
        var SeriesPins = GenerateSeriesPins(data.GetBytes(132, 612), 612 - 132);
        var ParallelPins = GenerateParallelPin(data.GetBytes(612, 812), data.GetBytes(812, 1012), 812 - 612);
        
        int places = 3;
        
        // Turns 
        if (data[1012] == 1)  // Turns.开关
        {
            int limitType = data[1014];
            string freq = data.GetBytes(1016, 1020).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
            string voltage = data.GetBytes(1020, 1024).ToFloat().KeepPlaces2(places);
            byte[] nominals = data.GetBytes(1024, 1184);
            byte[] mins = data.GetBytes(1344, 1504);
            byte[] maxs = data.GetBytes(1504, 1664);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                float nominal_float = nominals.GetBytes(i, i + 4).ToFloat();
                float min_float = 0f;
                float max_float = 0f;
                string min = string.Empty;
                string max = string.Empty;

                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min_float = mins.GetBytes(i, i + 4).ToFloat();
                    max_float = maxs.GetBytes(i, i + 4).ToFloat();
                    min = min_float == 0 ? string.Empty : $"{min_float.KeepPlaces2(places)}(T)";
                    max = max_float == 0 ? string.Empty : $"{max_float.KeepPlaces2(places)}(T)";
                }

                if (nominal_float != 0)
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        string nominal = $"{nominal_float.KeepPlaces2(3)}(T)";
                        if (SEC == 0)
                        {
                            transItemDatas.Add(
                                new TransItemData()
                                {
                                    Pri = PRI,
                                    Sec = SEC,
                                    Pins = pin,
                                    ParallelPin = ParallelPins[10 * PRI + SEC],
                                    SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 2)?.ShortPin,
                                    MinorTerm = "TURN",
                                    Freq = freq,
                                    Vol = voltage,
                                    Nominal = nominal,
                                    LowerLimit = string.Empty,
                                    UpperLimit = string.Empty,
                                }
                           );
                        }
                        else
                        {
                            transItemDatas.Add(
                                new TransItemData()
                                {
                                    Pri = PRI,
                                    Sec = SEC,
                                    Pins = pin,
                                    ParallelPin = ParallelPins[10 * PRI + SEC],
                                    SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 2)?.ShortPin,
                                    MinorTerm = "TURN",
                                    Freq = freq,
                                    Vol = voltage,
                                    Nominal = nominal,
                                    LowerLimit = min,
                                    UpperLimit = max,
                                }
                           );
                        }
                    }
                }
                i++; i++; i++;
            }
        }
        
        // Lx
        if (data[1664] == 1)
        {
            bool isLevelCurrent = false;
            isLevelCurrent = data[9976] == 1 ? true : false;  // LxsLevIsCur
            bool isMultiFreq = false;
            isMultiFreq = data[1667] == 1 ? true : false;    // lxs.多频开关
            bool isMultiVoltage = false;
            isMultiVoltage = data[9929] == 1 ? true : false;
            var multiFreqs = data.GetBytes(1678, 1838);  // lxs.多频
            int limitType = data[1668];   // lxs.极限模式
            string voltage = string.Empty;
            if (!isLevelCurrent)
            {
                voltage = data.GetBytes(1670, 1674).ToFloat().KeepPlaces2(places);  // lxs.电压
            }
            else
            {
                voltage = data.GetBytes(1670, 1674).ToFloat().UnitConversion(1000.0).KeepPlaces2(places);
            }

            string freq = data.GetBytes(1674, 1678).ToFloat().UnitConversion(0.001).KeepPlaces2(places);  // lxs.频率
            byte[] nominals = data.GetBytes(1998, 2158);  // lxs.标称
            byte[] mins = data.GetBytes(2318, 2478);    //lxs.下限
            byte[] maxs = data.GetBytes(2478, 2638);   // lxs.上限
            for (int i = 0; i < nominals.Length - 4; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                float nominal_float = nominals.GetBytes(i, i + 4).ToFloat();

                float min_float = 0f;
                float max_float = 0f;
                string min = string.Empty;
                string max = string.Empty;

                if (isMultiFreq)
                    freq = multiFreqs.GetBytes(i, i + 4).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min_float = mins.GetBytes(i, i + 4).ToFloat();
                    max_float = maxs.GetBytes(i, i + 4).ToFloat();


                    if (min_float != 0)
                    {
                        string[] formatVal = CommonUtil.FormatInductance(min_float, "H");
                        min = formatVal[0] + formatVal[1];
                        // min_float.HenryUnitConversion(GC.HenryUnit.H, out min);
                    }

                    if (max_float != 0)
                    {
                        string[] formatVal = CommonUtil.FormatInductance(max_float, "H");
                        max = formatVal[0] + formatVal[1];
                        // max_float.HenryUnitConversion(GC.HenryUnit.H, out max);
                    }
                    min = min_float == 0 ? string.Empty : min;
                    max = max_float == 0 ? string.Empty : max;
                }
                if (nominal_float != 0)
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        nominal_float.HenryUnitConversion(GC.HenryUnit.H, out string nominal);
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 1)?.ShortPin,
                                MinorTerm = "Lx",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }
        
        // Q   Qs
        if (data[1665] == 1)  // Qs.开关
        {
            byte[] nominals = data.GetBytes(2638, 2798);
            byte[] mins = data.GetBytes(2958, 3118);
            byte[] maxs = data.GetBytes(9769, 9929);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                string nominal = nominals.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
        
                string min = string.Empty;
                string max = string.Empty;
        
                // if (isMultiFreq)
                //     freq = multiFreqs.GetBytes(i, i + 4).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
                min = mins.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
                max = maxs.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
        
                if (nominal != 0.ToString($"F{3}"))
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        min = min == 0.ToString($"F{3}") ? string.Empty : min;
                        max = max == 0.ToString($"F{3}") ? string.Empty : max;
                        var lx = transItemDatas.Find(s => s.Pri == PRI && s.Sec == SEC && s.MinorTerm == "Lx");
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 1)?.ShortPin,
                                MinorTerm = "Q",
                                Freq = lx.Freq,
                                Vol = lx.Vol,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }

        // Lk
        if (data[5548] == 1)  // LKs.开关
        {
            bool isLevelCurrent = false;
            isLevelCurrent = data[9974] == 1 ? true : false;  // LksLevIsCur

            int limitType = data[5550];   // LKs.开关
            var multiFreqs = data.GetBytes(9688, 9728);
            var multiVoltages = data.GetBytes(9728, 9768);
            var LkShortPins = data.GetBytes(5600, 6320);  // LKs.shortpin
            string freq = data.GetBytes(5556, 5560).ToFloat().UnitConversion(0.001).KeepPlaces2(places);  // LKs.频率
            string voltage = string.Empty;
            if (!isLevelCurrent)
            {
                voltage = data.GetBytes(5552, 5556).ToFloat().KeepPlaces2(places);  // LKs.电压
            }
            else
            {
                voltage = data.GetBytes(5552, 5556).ToFloat().UnitConversion(1000.0).KeepPlaces2(places); // LKs.电压
            }

            byte[] testPins = data.GetBytes(5560, 5600);  // LKs.testpin
            var LkTestPins = GenerateLkTestPins(testPins);

            byte[] nominals = data.GetBytes(6320, 6480);  // LKs.标称
            byte[] mins = data.GetBytes(6640, 6800); // LKs.下限
            byte[] maxs = data.GetBytes(6800, 6960); // LKs.上限
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                float nominal_float = nominals.GetBytes(i, i + 4).ToFloat();
                float min_float = 0f;
                float max_float = 0f;
                string min = string.Empty;
                string max = string.Empty;
                float freqTemp = multiFreqs.GetBytes(4 * SEC, 4 * SEC + 4).ToFloat();
                if (freqTemp > 0)
                {
                    freq = freqTemp.UnitConversion(0.001).KeepPlaces2(places);
                }
                float volTemp = multiVoltages.GetBytes(4 * SEC, 4 * SEC + 4).ToFloat();
                if (volTemp > 0)
                {
                    voltage = volTemp.KeepPlaces2(places);
                }
                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min_float = mins.GetBytes(i, i + 4).ToFloat();
                    max_float = maxs.GetBytes(i, i + 4).ToFloat();
                    if (min_float != 0) min_float.HenryUnitConversion(GC.HenryUnit.H, out min);
                    if (max_float != 0) max_float.HenryUnitConversion(GC.HenryUnit.H, out max);
                    min = min_float == 0 ? string.Empty : min;
                    max = max_float == 0 ? string.Empty : max;
                }
                if (nominal_float != 0)
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        var shortPins = LkShortPins.GetBytes(480 * PRI + 48 * SEC, 480 * PRI + 48 * (SEC + 1));
                        string shortPin = string.Empty;
                        shortPin = GenerateLkPin(shortPins);

                        var LkPin = LkTestPins.Find(s => s.PRI == PRI && s.SEC == SEC);
                        int windingIndex = LkPin.Pin;
                        string testPin = pins1.Find(e => e.PRI == PRI && e.SEC == windingIndex).Pin;
                        nominal_float.HenryUnitConversion(GC.HenryUnit.H, out string nominal);
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = testPin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 0)?.ShortPin,
                                ShortCircuitPin = shortPin,
                                MinorTerm = "Lk",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }

        // DCR
        if (data[4738] == 1)  // Dcrs.开关
        {
            int limitType = data[4739];  // Dcrs.极限模式
            string freq = string.Empty;
            string voltage = string.Empty;
            byte[] nominals = data.GetBytes(4908, 5068);  // Dcrs.标称
            byte[] mins = data.GetBytes(5228, 5388);  // Dcrs.下限
            byte[] maxs = data.GetBytes(5388, 5548);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                float nominal_float = nominals.GetBytes(i, i + 4).ToFloat();
                float min_float = 0f;
                float max_float = 0f;
                string min = string.Empty;
                string max = string.Empty;

                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min_float = mins.GetBytes(i, i + 4).ToFloat();
                    max_float = maxs.GetBytes(i, i + 4).ToFloat();
                    if (min_float != 0) min_float.OhmUnitConversion(GC.OhmUnit.Ω, out min);
                    if (max_float != 0) max_float.OhmUnitConversion(GC.OhmUnit.Ω, out max);
                    min = min_float == 0 ? string.Empty : min;
                    max = max_float == 0 ? string.Empty : max;
                }
                if (nominal_float != 0)
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        nominal_float.OhmUnitConversion(GC.OhmUnit.Ω, out string nominal);
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 5)?.ShortPin,
                                MinorTerm = "DCR",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }

        // ACR
        if (data[3928] == 1)
        {
            int limitType = data[3929];
            string freq = data.GetBytes(3930, 3934).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
            string voltage = data.GetBytes(3934, 3938).ToFloat().KeepPlaces2(places);
            byte[] nominals = data.GetBytes(4098, 4258);
            byte[] mins = data.GetBytes(4418, 4578);
            byte[] maxs = data.GetBytes(4578, 4738);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                string nominal = nominals.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
                string min = 0.ToString($"F{3}");
                string max = 0.ToString($"F{3}");
                

                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min = mins.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
                    max = maxs.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
                }
                if (nominal != 0.ToString($"F{3}"))
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        min = min == 0.ToString($"F{3}") ? string.Empty : min;
                        max = max == 0.ToString($"F{3}") ? string.Empty : max;
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 3)?.ShortPin,
                                MinorTerm = "ACR",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                        );
                    }
                }
                i++; i++; i++;
            }
        }

        // Cx
        if (data[6960] == 1)
        {
            int limitType = data[6962];
            var CxPins1 = data.GetBytes(6972, 7692);
            var CxPins2 = data.GetBytes(7692, 8412);
            var ShortPins = data.GetBytes(12148, 12388);
            var multiFreqs = data.GetBytes(9527, 9567);
            var multiVoltages = data.GetBytes(9567, 9607);

            // updated by @suns 2022-04-18
            string voltage = data.GetBytes(6964, 6968).ToFloat().KeepPlaces2(places);
            string freq = data.GetBytes(6968, 6972).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
            byte[] nominals = data.GetBytes(8412, 8572);
            byte[] mins = data.GetBytes(8732, 8892);
            byte[] maxs = data.GetBytes(8892, 9052);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                string nominal = nominals.GetBytes(i, i + 4).ToFloat().UnitConversion(1000000000000000.0).KeepPlaces2(places);
                string min = 0.ToString($"F{3}");
                string max = 0.ToString($"F{3}");

                float freqTemp = multiFreqs.GetBytes(4 * SEC, 4 * SEC + 4).ToFloat();
                if (freqTemp > 0)
                {
                    freq = freqTemp.UnitConversion(0.001).KeepPlaces2(places);
                }
                float volTemp = multiVoltages.GetBytes(4 * SEC, 4 * SEC + 4).ToFloat();
                if (volTemp > 0)
                {
                    voltage = volTemp.KeepPlaces2(places);
                }

                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min = mins.GetBytes(i, i + 4).ToFloat().UnitConversion(1000000000000000.0).KeepPlaces2(places);
                    max = maxs.GetBytes(i, i + 4).ToFloat().UnitConversion(1000000000000000.0).KeepPlaces2(places);
                    min = (double.Parse(min) / 1000).ToString("f2") + "(pF)";
                    max = (double.Parse(max) / 1000).ToString("f2") + "(pF)";
                }
                if (nominal != 0.ToString($"F{3}"))
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        min = min == 0.ToString($"F{3}") ? string.Empty : min;
                        max = max == 0.ToString($"F{3}") ? string.Empty : max;
                        var testPins = CxPins1.GetBytes(180 * PRI + 18 * SEC, 180 * PRI + 18 * (SEC + 1))
                            .Append(CxPins2.GetBytes(180 * PRI + 18 * SEC, 180 * PRI + 18 * (SEC + 1)));
                        string testPin = string.Empty;
                        testPin = GenerateCxPin(testPins);
                        var shortPins = ShortPins.GetBytes(60 * PRI + 6 * SEC, 60 * PRI + 6 * (SEC + 1));
                        string shortPin = GenerateCxShortPin(shortPins);
             
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = testPin,
                                // ParallelPin = ParallelPins[10 * PRI + SEC],
                                ShortCircuitPin = shortPin,
                                MinorTerm = "Cx",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                        );
                    }
                }
                i++; i++; i++;
            }
        }
        
        // PS
        if (data[9052] == 1)  // Pss.开关
        {
            temp = data.GetBytes(9053, 9133);  // Pss.脚位
            for (int i = 0; i < temp.Length; i++)
            {
                int PRI = i / 20;
                int SEC = (i / 2) % 10;
                string pin = $"Short {temp[i].ToInt().ToString().PadLeft(3, '0')} - {temp[i + 1].ToInt().ToString().PadLeft(3, '0')}";
                if (temp[i] != 0)
                {
                    transItemDatas.Add(
                        new TransItemData()
                        {
                            Pri = PRI,
                            Sec = SEC,
                            Pins = pin,
                            MinorTerm = "PS",
                        }
                    );
                }
                i++;
            }
        }
        
        // Bls  BAL
        if (data[9134] == 1)  // Bls.开关
        {
            // int limitType = data[4739];  // Dcrs.极限模式
            string freq = string.Empty;
            string voltage = string.Empty;
            byte[] nominals = data.GetBytes(9216, 9296);  // Bls.标称
            byte[] maxs = data.GetBytes(9296, 9376);  // Bls.上限
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 20;
                int SEC = (i / 4) % 5;
                float nominal_float = nominals.GetBytes(i, i + 4).ToFloat();
                float min_float = 0f;
                float max_float = 0f;
                string max = string.Empty;
           
                max_float = maxs.GetBytes(i, i + 4).ToFloat();
                if (max_float != 0) max_float.HenryUnitConversion(GC.HenryUnit.H, out max);
                max = max_float == 0 ? string.Empty : max;
                
                if (nominal_float != 0)
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        nominal_float.HenryUnitConversion(GC.HenryUnit.H, out string nominal);
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 5)?.ShortPin,
                                MinorTerm = "BAL",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                // LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }
        
        // Zs
        if (data[3118] == 1)
        {
            int limitType = data[3119];
            string voltage = data.GetBytes(3124, 3128).ToFloat().KeepPlaces2(places);
            string freq = data.GetBytes(3120, 3124).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
            bool isMultiFreq = false;
            isMultiFreq = data[11198] == 1 ? true : false;
            bool isMultiVoltage = false;
            isMultiVoltage = data[11359] == 1 ? true : false;
            var multiFreqs = data.GetBytes(11199, 11359);
            var multiVoltages = data.GetBytes(11361, 11521);

            byte[] nominals = data.GetBytes(3288, 3448);
            byte[] mins = data.GetBytes(3608, 3768);
            byte[] maxs = data.GetBytes(3768, 3928);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                string nominal = nominals.GetBytes(i, i + 4).ToFloat().UnitConversion(1.0).KeepPlaces2(places);
                string min = 0.ToString($"F{3}");
                string max = 0.ToString($"F{3}");
                if (isMultiFreq) freq = multiFreqs.GetBytes(i, i + 4).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
                if (isMultiVoltage) voltage = multiVoltages.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);

                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min = mins.GetBytes(i, i + 4).ToFloat().UnitConversion(1.0).KeepPlaces2(places);
                    max = maxs.GetBytes(i, i + 4).ToFloat().UnitConversion(1.0).KeepPlaces2(places);
                }

                if (nominal != 0.ToString($"F{3}"))
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        min = min == 0.ToString($"F{3}") ? string.Empty : min;
                        max = max == 0.ToString($"F{3}") ? string.Empty : max;
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 4)?.ShortPin,
                                MinorTerm = "Zx",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }
        
        testConditions.BeautifyData = transItemDatas;
    }
}